package com.example.securityproject.demos.common;

import lombok.Data;

/**
 * @Author: Jiangjb
 * @CreateTime: 2022-06-24  21:32
 * @Description: 接口统一返回类
 */
@Data
public class CommonResult<T> {
    private Boolean success;
    private int code;
    private String message;
    private T data;

    public CommonResult() {
    }

    //定义统一类型返回
    public CommonResult(ResultCode resultCode, Boolean success, T data) {
        this.success = success;
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    /*
     * 自定义异常 构造函数
     **/
    public CommonResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    //因为重复使用成功与错误，所以建立了这两个，方便使用
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(ResultCode.HTTP_SUCCESS, true, data);
    }

    public static <T> CommonResult<T> success() {
        return new CommonResult<>(ResultCode.HTTP_SUCCESS, true, null);
    }

    public static CommonResult error() {
        return new CommonResult<>(ResultCode.HTTP_FAIL, false, null);
    }

    /*
     * 自定义异常
     **/
    public static CommonResult error(int code, String msg) {
        return new CommonResult<>(code, msg);
    }

    public static CommonResult error(String msg) {
        return new CommonResult<>(ResultCode.HTTP_FAIL, false, msg);
    }

    public static CommonResult error(ResultCode resultCode) {
        return new CommonResult<>(resultCode, false, null);
    }
}

